<?php
namespace app\admin\controller;
use think\Controller;
use catetree\Catetree;
class Article extends Controller
{
    public function lst()
    {
        $articleres=db('article')
                   ->field('a.*,b.cate_name')
                   ->alias('a')
                   ->join('cate b',"a.cate_id=b.id")
                   ->order('a.id desc')
                   ->paginate(5);
        $this->assign('articleres',$articleres);
        return view();
    }
    public function add()
    {

        if(request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
//            dump($data);die;
            if ($data['link_url'] && stripos($data['link_url'], 'http://') === false) {
                $data['link_url'] = 'http://' . $data['link_url'];
            }
            //处理图片上传
            if ($_FILES['thumb']['tmp_name']) {
                $data['thumb'] = $this->upload();
            }
            //添加验证数据
            $validate = validate('article');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $add = db('article')->insert($data);
            if ($add) {
                $this->success('添加文章成功', url('article/lst'));
            } else {
                $this->error('添加文章失败，请返回修改', url('article/add'));
            }
            return;
        }
            $cate=new Catetree();
            $cateRes=db('cate')->order('sort DESC')->select();
            $cateRes=$cate->catetree($cateRes);

            $this->assign([
                'cateRes'=>$cateRes,

            ]);

        return view();
    }
    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.');
//            dump($data);die;
            if($data['link_url'] && stripos($data['link_url'],'http://')===false){
                $data['link_url']='http://'.$data['link_url'];
            }
            //修改模块处理图片上传
            if($_FILES['thumb']['tmp_name']){
                $oldarticles=db('article')->field('thumb')->find($data['id']);
                $oldBrangImg=IMG_UPLOADS.$oldarticles['thumb'];
                if(file_exists($oldBrangImg)){
                    @unlink($oldBrangImg);
                }

                 $data['thumb']=$this->upload();
//                dump($$data);die;
            }
            //添加验证数据
            $validate = validate('article');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $save=db('article')->update($data);
            if($save !==false){
                $this->success('修改文章成功',url('article/lst'));
            }else{
                $this->error('修改文章失败，请返回修改',url('article/add'));
            }
            return;
        }
        $id=input('id');
        $arts=db('article')->find($id);
        $cate=new Catetree();
        $cateRes=db('cate')->order('sort DESC')->select();
        $cateRes=$cate->catetree($cateRes);



        $this->assign([
            'cateRes'=>$cateRes,
            'arts'=>$arts,

        ]);
//     dump($arts);die();
        return view();
    }
    public function del()
    {
        $id=input('id');
        $article=db('article');
        $arts=$article->field('thumb')->find($id);
        $thumbSrc=IMG_UPLOADS.$arts['thumb'];
        if(file_exists($thumbSrc)){
            @unlink($thumbSrc);
        }
        $del=$article->delete($id);
        if($del){
            $this->success('删除数据成功','lst');
        }else{
            $this->error('删除数据失败');
        }
        return ;
    }
    //编辑器的图片管理
    public function imglist(){
        $_files=my_scandir();
        $files=array();
        foreach ($_files as $k=>$v){
            if(is_array($v)){
                foreach ($v as $k1=>$v1){
                    $v1=str_replace(UEDITOR,HTTP_UEDITOR,$v1);
                    $files[]=$v1;
                }
            }else{
                $v=str_replace(UEDITOR,HTTP_UEDITOR,$v);
                $files[]=$v;
            }
        }
        $this->assign([
           'imgRes'=>$files,
        ]);
        return view();
    }
    public function delimg(){
        $imgsrc=input('imgsrc');
        $imgsrc=DEL_UEDITOR.$imgsrc;
        if(file_exists($imgsrc)){
            if (@unlink($imgsrc)){
                echo 1;
            }else{
                echo 2;
            }
        }else{
            echo 3;
        }
}
    //单独上传图片源码
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('thumb');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS .'static'. DS .'uploads');
            if($info){
                return  $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();  die;
            }
        }
    }
}
